package com.example.x.dnuifood_929dx.controller;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.x.dnuifood_929dx.R;
import com.example.x.dnuifood_929dx.adapter.FoodListAdapter;
import com.example.x.dnuifood_929dx.bean.CollectStatus;
import com.example.x.dnuifood_929dx.bean.FoodBean;
import com.example.x.dnuifood_929dx.bean.ResultBean;
import com.example.x.dnuifood_929dx.listener.onRetrofitListener;
import com.example.x.dnuifood_929dx.model.ShopModel;

import java.util.List;

public class FoodListActivity extends BaseActivity implements onRetrofitListener, View.OnClickListener {
    //声明界面中需要的所有控件 View
    private Context context;
    private String shop_id,user_id;
    private  String shop_name="",result="",phonenum="";
    private TextView shopname;
    private ImageButton go_back,collect;
    private List<FoodBean>list;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private FoodListAdapter adapter;
    private int page=1; // 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置


    @Override
    void initViews() {
        setLayout(R.layout.foodlist);

        //初始化界面控件 View
        shopname=findViewById(R.id.textView9);
        go_back=findViewById(R.id.imageButton);
        collect=findViewById(R.id.imageButton);
        recyclerView = findViewById(R.id.food_list);
        context=FoodListActivity.this;
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);
    }

    @Override
    void initEvents() {
        go_back.setOnClickListener(this);
        collect.setOnClickListener(this);
        shop_name=getIntent().getStringExtra("shop_name");
        shop_id=getIntent().getStringExtra("shop_id");

        //shop_id2="2";
        shopname.setText(shop_name);
//        ShopModel foodModel=new ShopModel();
//        foodModel.getFoodByShop(shop_id,this);
        //处理【返回】和【收藏】按钮的点击事件
        //【返回】按钮调用 finish()关闭当前界面
        //【收藏】按钮调用 collect()方法
    }

    @Override
    void initData() {

        //通过 Intent 获取店铺名称
        //      shop_id=getIntent().getStringExtra("shop_id");
        //获取店铺的菜谱列表，调用 getFoodList ()方法
        getFoodList();
        //获取店铺的收藏状态，调用 iscollected ()方
        iscollected();

    }


    //发送获取店铺的菜谱列表的网络请求
    private void getFoodList() {
        ShopModel foodModel=new ShopModel();
        foodModel.getFoodByShop(shop_id,this);
    }
    //发送获取店铺的收藏状态的网络请求
    private void iscollected(){
        ShopModel foodModel=new ShopModel();
        foodModel.isCollected(getUser_id(), shop_id,"0",this);
    }
    //发送对店铺的收藏信息进行修改的网络请求
    private void collect(){
        ShopModel foodmodel= new ShopModel();
        foodmodel.userCollectShop(getUser_id(), shop_id, this);
    }
    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag)
        {
            case 2:
                //获取店铺的菜谱列表

                adapter = new FoodListAdapter(FoodListActivity.this, (List<FoodBean>)o, R.layout.shop_item2,phonenum);
                recyclerView.setAdapter(adapter);
                break;

            case 3:
                //获取店铺的收藏状态
                result=((CollectStatus)o).getCollected().toString();
                if (result.equals("0")) {
                    collect.setImageResource(R.drawable.ic_collection_fill);
                }
                else {
                    collect.setImageResource(R.drawable.ic_collection_fill);
                }
                break;
            case 4:
//            店铺的收藏信息进行修改
                if (((ResultBean)o).getSuccess().equals("1"))  {
                    if (result.equals("0")) {
                        collect.setImageResource(R.drawable.ic_collection_fill);
                        Toast.makeText(FoodListActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        collect.setImageResource(R.drawable.ic_collection_fill);
                        Toast.makeText(FoodListActivity.this, "取消收藏成功",
                                Toast.LENGTH_SHORT).show();
                        result = "0";
                    }
                } else {
                    Toast.makeText(FoodListActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }



    @Override
    public void onFailure(String msg) {
        Toast.makeText(FoodListActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imageButton3:
                finish();
                break;
            case R.id.imageButton:
                collect();
                break;
        }
    }}